package com.shengqi.xinnongren.work.ui.home.presenter;

import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.home.view.MainView;

import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/7/10.
 */
public class MainPresenter extends BasePresenter<MainView> {
    public void getSplashContent() {
        JSONObject jsonData = Constants.getJsonData(null, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getSplashContent(requestBody), getView()::showSplashContent);
    }

    public void checkVersion() {
        JSONObject jsonData = Constants.getJsonData(null, null);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().checkVersion(requestBody), response -> getView().showVersionInfo(response));
    }

    public void getShoppingCartNum() {
        JSONObject jsonData = Constants.getJsonData(null, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getShoppingCartNum(requestBody), s -> {
            HttpRespond<String> httpResult = Global.getHttpResult(s);
            if (httpResult.result == 1) {
                int num = Integer.parseInt(httpResult.data);
                getView().onGetShoppingCartNum(num);
            }
        });
    }
}
